package com.atguigu.order.controller;
import java.math.BigDecimal;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.order.bean.Order;
import com.atguigu.order.properties.OrderProperties;
import com.atguigu.order.service.OrderService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

//@RefreshScope //支持nacos动态刷新配置
//@RequestMapping("/api/order/")
@RestController
public class OrderController {

    @Resource
    OrderService orderService;

    @Resource
    private OrderProperties orderProperties;

//    @Value("${order.timeout}")
//    String orderTimeout;
//    @Value("${order.auto-confirm}")
//    String orderAutoConfirm;

    @GetMapping("create")
    public Order create(@RequestParam("userId") Long userId,
                        @RequestParam("productId") Long productId ){
        Order order = orderService.create(userId,productId);
        return order;
    }

    @SentinelResource(value = "seckill-order",fallback = "seckillFallback")
    @GetMapping("seckill")
    public Order seckill(@RequestParam("userId") Long userId,
                        @RequestParam("productId") Long productId ){
        Order order = orderService.create(userId,productId);
        order.setId(Long.MAX_VALUE);
        return order;
    }

    public Order seckillFallback(Long userId, Long productId, Throwable e){
        System.out.println("热点兜底方法执行了。。。。。");
        Order order = new Order();
        order.setId(0L);
        order.setTotalAmount(new BigDecimal("0"));
        order.setNickName("未知数据");
        order.setUserId(userId);
        order.setAddress("错误信息"+e.getClass());
        return order;
    }

    @GetMapping("config")
    public String create(){

        return "orderTimeout"+orderProperties.getTimeout()
                +" orderAutoConfirm"+orderProperties.getAutoConfirm()
                +" orderDburl"+orderProperties.getDburl();
    }

    @GetMapping("getA")
    public String getA(){
        return "A";
    }

    @GetMapping("getB")
    public String getB(){
        return "B";
    }
}
